package com.yunbao.hujin.myzoomcenter.banner_two.utils;

import android.annotation.SuppressLint;
import android.app.Application;
import android.content.Context;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;

/**
 * DisplayUtil on 2016/11/17.
 */

public class DisplayUtil {
    public float mDensity = -1.0F;
    public float mHeightPix = -1.0F;
    public float mWidthPix = -1.0F;

    private DisplayUtil() {
        initParams();
    }

    private static class SingletonHolder {
        private static final DisplayUtil INSTANCE = new DisplayUtil();
    }

    public static DisplayUtil getInstance() {
        return SingletonHolder.INSTANCE;
    }

    @SuppressLint("NewApi")
    private void initParams() {
        if (mDensity < 0) {
            DisplayMetrics displayMetrics = new DisplayMetrics();
//            WindowManager wManager = (WindowManager) App.app.getSystemService(Context.WINDOW_SERVICE);
//            wManager.getDefaultDisplay().getRealMetrics(displayMetrics);

            mDensity = displayMetrics.density;
            mWidthPix = displayMetrics.widthPixels;
            mHeightPix = displayMetrics.heightPixels;
            if (mWidthPix > mHeightPix) { //portrite view: w < h
                float temp = mWidthPix;
                mWidthPix = mHeightPix;
                mHeightPix = temp;
            }
//            Log.d("AA", "宽：" + mWidthPix + "\n高：" + mHeightPix);
        }
    }

    public void setViewLayout(View view, int w) {
        ViewGroup.LayoutParams lm = view.getLayoutParams();
        lm.width = w;
        view.setLayoutParams(lm);
    }
}
